﻿//-----------------------------------------------------------------------
// <copyright file="MrsTelemetryServiceProvider.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>Telemetry service provider for sending url usage report and url error report</summary>
// <author>v-haiwan</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading;
using Microsoft.Reputation.Services;
using Microsoft.Reputation.Tests.FrontEndCommon;

namespace Microsoft.Reputation.Tests.ServiceabilityTest.Helper
{
    /// <summary>
    /// Container of Telemetry data
    /// </summary>
    public class MrsTelemetryServiceProvider
    {
        /// <summary>
        /// samplingRate field
        /// </summary>
        private uint m_samplingRate;

        /// <summary>
        /// communicationDelayMinutes field
        /// </summary>
        private uint m_communicationDelayMinutes;

        /// <summary>
        /// clientIdToken field
        /// </summary>
        private ClientIdToken m_clientIdToken;

        /// <summary>
        /// Telemetry Client object
        /// </summary>
        private MrsTelemetryServiceClient m_client;

        /// <summary>
        /// urlTelemetry data property
        /// </summary>
        private UrlTelemetry m_urlUsageReport;

        /// <summary>
        /// urlErrorReports property
        /// </summary>
        private UrlErrorReport[] m_urlErrorReports;

        /// <summary>
        /// Initializes a new instance of the MrsTelemetryServiceProvider class
        /// </summary>
        /// <param name="serviceURL">telemetry web service URL</param>
        public MrsTelemetryServiceProvider(string serviceURL)
        {
            m_client = new MrsTelemetryServiceClient(CreateBinding(serviceURL), new EndpointAddress(serviceURL));
            CreateNewClientIdToken();
        }

        /// <summary>
        /// Call SendUrlTelemetry
        /// </summary>
        /// <returns>SendUrlTelemetry result</returns>
        public uint SendUrlUsageReports()
        {
            InitializeUrlUsageReport(true);
            return m_client.SendUrlUsageReport(out m_samplingRate, out m_communicationDelayMinutes, m_clientIdToken, m_urlUsageReport);
        }

        /// <summary>
        /// Call SendUrlErrorReports
        /// </summary>      
        /// <returns>SendUrlErrorReports result</returns>
        public uint SendUrlErrorReports()
        {
            InitializeUrlErrorReport(true);
            return m_client.SendUrlErrorReports(out m_samplingRate, out m_communicationDelayMinutes, m_clientIdToken, m_urlErrorReports);
        }

        /// <summary>
        /// Initialize Url Usage report data
        /// </summary>
        /// <param name="createObject">create Object</param>
        private void InitializeUrlUsageReport(bool createObject)
        {
            if (createObject)
            {
                m_urlUsageReport = new UrlTelemetry();
                m_urlUsageReport.StartTime = DateTime.Now;
                m_urlUsageReport.BlockOverruleList = TelemetryObjectHelper.CreateValidBlockOverrules();
                m_urlUsageReport.CategoryCountList = TelemetryObjectHelper.CreateValidCategoryCounts();
                m_urlUsageReport.CategoryPrecedenceCountList = TelemetryObjectHelper.CreateValidCategoryCounts();
                m_urlUsageReport.OverrideExceptionList = TelemetryObjectHelper.CreateValidOverrideExceptions();
                m_urlUsageReport.SampleUrls = TelemetryObjectHelper.CreatetSampleUrls();
                m_urlUsageReport.UrlCounts = TelemetryObjectHelper.CreateValidUrlCounts();
                Thread.Sleep(2000);
                m_urlUsageReport.EndTime = DateTime.Now;
            }
            else
            {
                m_urlUsageReport = null;
            }
        }
                
        /// <summary>
        /// Initialize UrlErrorReport
        /// </summary>
        /// <param name="createObject">create Object</param>
        private void InitializeUrlErrorReport(bool createObject)
        {
            if (createObject)
            {
                m_urlErrorReports = TelemetryObjectHelper.CreateValidErrorReports();
            }
            else
            {
                m_urlErrorReports = null;
            }
        }

        /// <summary>
        /// Make ClientIdToken Object
        /// </summary>
        private void CreateNewClientIdToken()
        {
            m_clientIdToken = new ClientIdToken();
            m_clientIdToken.ClientId = new Guid("4228ac1a-2e5c-40f1-b193-aa5f078c8e83");      // DO NOT CHANGE - This is the test GUID being used and BackEnd checks for this GUID to separate test data.
            m_clientIdToken.CustomerId = new Guid("3fca792f-bbb7-4d38-83e4-1993a994f6a4");    // DO NOT CHANGE - This is the test GUID being used and BackEnd checks for this GUID to separate test data.
            m_clientIdToken.CustomerVersion = Environment.OSVersion.VersionString;
            m_clientIdToken.DeploymentId = new Guid("59867f97-e895-4397-b1e7-5d7c1326da48");  // DO NOT CHANGE - This is the test GUID being used and BackEnd checks for this GUID to separate test data.
        }

        /// <summary>
        /// Helper method to create telemetry service connection binding.
        /// </summary>
        /// <param name="serviceURL">telemetry web service URL</param>
        /// <returns>binding object</returns>
        private Binding CreateBinding(string serviceURL)
        {
            BasicHttpBinding binding = new BasicHttpBinding();

            binding.MaxReceivedMessageSize = 16777216;
            binding.SendTimeout = new TimeSpan(2, 0, 0);
            binding.ReceiveTimeout = new TimeSpan(2, 0, 0);

            if (serviceURL.StartsWith("https:"))
            {
                binding.Security.Mode = BasicHttpSecurityMode.Transport;
            }
            else
            {
                binding.Security.Mode = BasicHttpSecurityMode.None;
            }

            return binding;
        }
    }
}
